package cl.jsoftlabs.gwt.soccer.client.mvc.views;

import cl.jsoftlabs.gwt.soccer.client.bean.AppPestana;
import cl.jsoftlabs.gwt.soccer.client.mvc.events.AppEvents;
import cl.jsoftlabs.gwt.soccer.client.ui.futbolclub.FutbolClubForm;
import cl.jsoftlabs.gwt.soccer.client.ui.futbolclub.FutbolClubGrid;

import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;

public class FutbolClubView extends View {

	public FutbolClubView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType eventType = event.getType();
		if (eventType.equals(AppEvents.FutbolClubNuevo)) {
			onFutbolClubNuevo(event);
		}else if (eventType.equals(AppEvents.mostrarTablaClubes)) {
			onMostrarGridClubes(event);
		} else if (eventType.equals(AppEvents.FutbolClubSeleccionado)) {
			onFutbolClubSeleccionado(event);
		} else if (eventType.equals(AppEvents.FutbolClubModificar)) {
			onFutbolClubModificar(event);
		}
	}

	private void onFutbolClubNuevo(AppEvent event) {
		FutbolClubForm form = new FutbolClubForm();
		form.show();
	}
	
	private void onFutbolClubModificar(AppEvent event) {
		FutbolClubForm form = new FutbolClubForm();
		form.setIdFC(Integer.parseInt( String.valueOf( event.getData() ) ));
		form.show();
	}

	private void onFutbolClubSeleccionado(AppEvent event) {
		Info.display("FC Seleccionado", String.valueOf( event.getData() ) );
	}

	private void onMostrarGridClubes(AppEvent event) {
		LayoutContainer contenedor = new LayoutContainer();
		String nombre = event.getData();
		final FutbolClubGrid itemGrid = new FutbolClubGrid();
		contenedor.add(itemGrid);
		Dispatcher.forwardEvent(AppEvents.AppNuevaPestana, new AppPestana(nombre,itemGrid));
		//Dispatcher.forwardEvent(AppEvents.AppNuevaPestana, new AppPestana("Tree",new FutbolClubTreeGrid()));
	}

}
